home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
lisp
/
eulisp
/
mpfeel.lha
/
MPFeel
/
Plurals
/
Random
/
fp_random.m
< prev
next >
Wrap
Text File
|
1992-05-12
|
2KB
|
70 lines
/******************************************************************************
A portable plural random number generator based on three linear
congruential generators. -- Numerical Recipes in C pp. 210
Copyright 1991,1992 Lee Iverson
Author:
Lee Iverson <leei@mcrcim.mcgill.edu>,
McGill Research Centre for Intelligent Machines (McRCIM)
See "copyright.h" for complete copyright information.
******************************************************************************/
static char RCSid[] =
"$Id: fp_random.m,v 1.2 92/01/23 17:31:47 leei Exp $";
#include <values.h>
#include <math.h>
#include "p_randomI.h"
plural float fp_frandom _IMPL_VOID()
{
register plural float tmp;
register plural int j;
if ( !rand_init__ ) init_p_random(1 /* Was 0 */);
ix1__ = (IA1*ix1__ + IC1) % M1;
ix2__ = (IA2*ix2__ + IC2) % M2;
ix3__ = (IA3*ix3__ + IC3) % M3;
j = 1 + ((97*ix3__)/M3);
tmp = r__[j];
r__[j] = (ix1__+ix2__*RM2)*RM1;
return tmp;
}
plural float
fp_uniform_random _IMPL((lo, hi),
register plural float lo _AND
register plural float hi)
{
return lo + fp_frandom()*(hi-lo);
}
plural float
fp_normal_random _IMPL((stdev), plural float stdev)
{
static int iset = 0;
static plural float gset;
plural float fac, r, v1, v2;
if ( !iset ) {
do {
v1 = fp_uniform_random((plural float)-1.0,(plural float)1.0);
v2 = fp_uniform_random((plural float)-1.0,(plural float)1.0);
r = v1*v1 + v2*v2;
} while ( r >= 1.0 );
fac = fp_sqrt(-2.0*fp_log(r)/r);
gset = v1*fac; ++iset;
return v2*fac*stdev;
} else {
--iset;
return gset*stdev;
}
}